Professor Details or My Book on Writing Grammars:

/**My long SWI-Prolog grammar keeps on failing
https://stackoverflow.com/questions/52559339/my-long-swi-prolog-grammar-keeps-on-failing

My long grammar keeps on failing. It recognises text in the form " a.\n a." and is supposed to return a list of sentences, spaces and returns. Test is the main grammar that calls the other rules and compiles the output, sentence32 and sentence33 recognises the letters, etc. in the sentence, spaces and returns recognise the space and returns respectively and sentenceendpunctuation parses the end of sentence punctuation. Here it is:**/

use_module(library(dcg/basics)).

test([]) --> [].  
test(AAs) --> spaces(Y), {string_length(Y,YLen),
(YLen>0->Start=[[s,YLen]];Start=[])}, sentence32(X), returns(Z),
{[XS]=X, string_length(XS,XLen),
(XLen>0->append(Start,[X],Next);Next=Start), string_length(Z,ZLen),
(ZLen>0->append(Next,[[r,ZLen]],Last);Last=Next)},
test(As),{append(Last,As,AAs)},!. 

test(Last) --> spaces(Y), {string_length(Y,YLen),
(YLen>0->Start=[[s,YLen]];Start=[])}, sentence32(X), returns(Z),
{[XS]=X, string_length(XS,XLen),
(XLen>0->append(Start,[X],Next);Next=Start), string_length(Z,ZLen),
(ZLen>0->append(Next,[[r,ZLen]],Last);Last=Next)},!.

spaces(XXs) --> [X], {X=32}, spaces(Xs),
{char_code(Ch,X),atom_string(CA,Ch),atom_concat(CA,Xs,XXs)}, !.
spaces('') --> [].

returns(XXs) --> [X], {X=10}, returns(Xs),
{char_code(Ch,X),atom_string(CA,Ch),atom_concat(CA,Xs,XXs)}, !.
returns('') --> [].

sentence32([XsZ]) --> sentence33(Xs), sentenceendpunctuation(Z),
{atom_string(CA,Xs),atom_concat(CA,Z,XsZ)}, !. sentence32('') --> [].

sentence33(CXs) --> [X],
{((char_type(X,alnum);char_type(X,white));char_type(X,punct)),
not(X=93),char_code(C,X) , not(X=91) , not(X=46) , not(X=33) ,
not(X=63) , not(X=10) }, sentence33(Xs),
{atom_string(CA,C),atom_concat(CA,Xs,CXs)}, !. 
sentence33('') --> [].

sentenceendpunctuation(Z) --> [Z1],
{char_code(Z,Z1),(Z='.';(Z='?';(Z='!')))}, !.
%%When I enter the query to parse my string

%%string_codes(" a. a.",Y),phrase(test(X),Y).
%%I get false.

/**
SOLVED: Atom Concat in Recursive SWI-Prolog Grammar
https://stackoverflow.com/questions/52562459/solved-atom-concat-in-recursive-swi-prolog-grammar

SOLVED: Atom Concat in Recursive SWI-Prolog Grammar

I finally solved my grammar!  Here is how to write a recursive grammar in SWI-Prolog with atom concat.  It basically replaces [] with '' in the base case.

    spaces(XXs) --> [X], {X=32}, spaces(Xs),    
    {char_code(Ch,X),atom_string(CA,Ch),atom_concat(CA,Xs,XXs)}, !. 
    spaces('') --> [].

It has the desired output:

    ?- string_codes("   ",Y),phrase(spaces(X),Y).
    Y = [32, 32, 32],
    X = '   '.

    **/